<template>
  <div class="goods">
    <el-card class="box-card" v-if="status === 'index'">
      <el-row>
        <el-button
          type="primary"
          size="mini"
          @click="getData"
          v-if="has('settings/show')"
          >刷新</el-button
        >
      </el-row>

      <el-form
        ref="form"
        :rules="rules"
        :model="editParams"
        label-width="150px"
        :style="{ width: '90%' }"
      >
        <el-form-item prop="name" label="app名称">
          <el-input v-model="editParams.name" placeholder="app名称"> </el-input>
        </el-form-item>
        <el-form-item prop="commission_probability" label="佣金概率">
          <el-input v-model="editParams.commission_probability" placeholder="佣金概率">
          </el-input>
        </el-form-item>
        <el-form-item label="福袋主播中大奖" prop="box_bag_winner">
          <el-select v-model="editParams.box_bag_winner">
            <el-option
              v-for="item in statusArr"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="android_url" label="安卓下载地址">
          <el-input v-model="editParams.android_url" placeholder="安卓下载地址">
          </el-input>
        </el-form-item>
        <el-form-item prop="ios_url" label="ios下载地址">
          <el-input v-model="editParams.ios_url" placeholder="ios下载地址">
          </el-input>
        </el-form-item>
        <el-form-item label="客服" prop="customer">
          <el-button
            size="mini"
            type="primary"
            @click="openImage(2, false, extra)"
            >选择图片</el-button
          >
          <div>
            <el-image
              v-if="editParams.customer"
              :src="editParams.customer"
              :preview-src-list="[editParams.customer]"
              style="height: 200px"
              fit="contain"
            >
            </el-image>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submit('form')"
            v-if="has('settings/update')"
            >保 存</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>

    <SimpleImageUpload
      :cardStatus="cardStatus"
      :imageFormatArr="imageFormatArr"
      :imageData="imageData"
      :imageLoading="imageLoading"
      :uploadImageAction="uploadImageUrl"
      :multiple="multiple"
      @uploadImageSuccess="uploadFileSuccess"
      @uploadImageBefore="uploadImageBefore"
      @changeImageSize="changeImageSize"
      @changeImagePage="changeImagePage"
      @changeImageFormat="changeImageFormat"
      @choiceImageConfirm="choiceImageConfirm"
      @closeImage="closeImage"
      @getImages="getImages"
    ></SimpleImageUpload>
  </div>
</template>

<script>
import api from "../../api/api.js";
import SimpleImageUpload from "@/components/SimpleImageUpload/index.vue";
export default {
  name: "setting",
  components: {
    SimpleImageUpload
  },
  data() {
    return {
      status: "index", // 页面控制
      loading: false, // 全局加载中
      pageSizes: [10, 20, 50, 100, 200, 500, 800, 1000, 2000], // 每页条数默认10
      searchParams: {
        page: 1,
        limit: 10
      },
      editParams: {},
      rules: {
        name: [{ required: true, message: "请输入app名称", trigger: "blur" }],
        android_url: [
          { required: true, message: "请输入安卓下载地址", trigger: "blur" }
        ],
        ios_url: [
          { required: true, message: "请输入ios下载地址", trigger: "blur" }
        ],
        customer: [
          { required: true, message: "请上传客服图片", trigger: "blur" }
        ]
      },
      cardStatus: false,
      imageFormatArr: [
        { id: "", title: "全部" },
        { id: 1, title: "商品图片" },
        { id: 2, title: "商品详情图" },
        { id: 3, title: "banner" },
        { id: 4, title: "活动" },
        { id: 5, title: "盲盒" }
      ],
      statusArr: [
        { id: 1, title: "开启" },
        { id: 0, title: "关闭" }
      ],
      imageData: {},
      imageLoading: false,
      uploadImageUrl: "/admin/common/upload?format=3",
      multiple: false,
      scene: 1, // 打开组件场景
      extra: {}
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 加载中
    load() {
      this.loading = this.$loading({
        lock: true,
        text: "数据加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    },
    // 取消加载
    unload() {
      this.loading.close();
    },
    uploadImageBefore() {
      this.load();
    },
    closeImage() {
      this.cardStatus = false;
    },
    // 改变图片组件每页条数时
    changeImageSize(limit) {
      this.imageParams.limit = limit;
      this.getImages();
    },
    // 改变图片组件当前页码时
    changeImagePage(page) {
      this.imageParams.page = page;
      this.getImages();
    },
    openImage(scene, multiple, extra) {
      this.multiple = multiple; // 单选还是多选
      this.scene = scene;
      this.extra = extra; // 额外数据
      this.cardStatus = true;
      this.getImages();
    },
    // 组件确定选择图片
    choiceImageConfirm(choiceImages) {
      if (this.scene == 1) {
        this.editParams.goods_images = this.editParams.goods_images.concat(
          choiceImages
        );
      } else if (this.scene == 2) {
        this.editParams.customer = choiceImages[0];
      } else if (this.scene == 3) {
        this.extra.row.image = choiceImages[0];
      } else if (this.scene == 4) {
        var html = "";
        choiceImages.map(item => {
          html += `<div><img src="${item}" /></div>`;
        });
        this.setHtml(html);
        console.log("html:", html);
      }
    },
    // 组件图片上传成功取消遮罩重新请求数据
    uploadFileSuccess(res) {
      this.unload();
      this.getImages();
      this.$message.success(res.msg);
    },
    // 选择类型筛选图片
    changeImageFormat(type) {
      this.initImage(type);
      this.getImages();
    },
    // 查看图片
    getImages() {
      this.cardStatus = true;
      this.imageLoading = true;
      api.images
        .index({ params: this.imageParams })
        .then(res => {
          this.imageLoading = false;
          if (res.data.code == 200) {
            this.imageData = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 重置图片搜索参数
    initImage(type) {
      this.imageParams.format = type;
      this.imageParams.page = 1;
    },
    // 获取所有
    getData() {
      this.load();
      api.settings
        .show()
        .then(res => {
          this.unload();
          if (res.data.code === 200) {
            this.editParams = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 重置搜索参数重新搜索
    reset() {
      this.init();
      this.getData();
    },
    // 初始化
    init() {
      this.editParams = {};
      this.status = "index";
    },
    // 改变每页条数时
    changeSize(limit) {
      this.searchParams.limit = limit;
      this.getData();
    },
    // 改变当前页码时
    changePage(page) {
      this.searchParams.page = page;
      this.getData();
    },
    // 添加页面
    add() {
      this.status = "add";
    },
    // 保存添加或编辑
    submit(formName) {
      // 自动验证
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          this.load();
          api.settings
            .edit(this.editParams)
            .then(res => {
              this.unload();
              if (res.data.code === 200) {
                this.$message.success(res.data.msg);
                this.reset();
              }
            })
            .catch(error => {
              console.log(error);
            });
        } else {
          this.$message.error(Object.values(error)[0][0].message);
          return false;
        }
      });
    }
  }
};
</script>

<style scoped lang="less">
.goods {
  margin: 0 auto;
  .block {
    margin-bottom: 10px;
    padding-left: 10px;
    border-left: 5px solid #ccc;
    background: #f5f8fc;
    height: 50px;
    line-height: 50px;
    color: #ccc;
  }
  .search {
    margin: 0 auto 10px auto;
  }
  .img_box {
    display: flex;
    align-items: center;
    justify-content: space-around;
    img {
      width: 60px;
      height: 60px;
      cursor: pointer;
    }
  }
}
.el-input {
  width: 600px;
}
</style>
